# -*- coding: utf-8 -*-
# @Time    : 18-8-4 上午10:45
# @Author  : hp
# @Email   : shaoeric@foxmail.com

import requests
import json


def getresponse(msg, userId):
    url = "http://openapi.tuling123.com/openapi/api/v2"
    headers = {
        'content-type': 'application/json',
        'charset': 'utf-8'
    }
    data = {
        "perception":
            {
                "inputText":
                    {
                        "text": msg
                    },

                "selfInfo":
                    {
                        "location":
                            {
                                "city": "无锡"
                            }
                    }
            },

        "userInfo":
            {
                "apiKey": "bff90f07b67f4657a55d2931e887f2fe",
                "userId": userId
            }
    }
    data = json.dumps(data).encode('utf-8')
    response = requests.post(url=url, data=data, headers=headers).text
    data = json.loads(response)

    if "parameters" in data['intent'] and "recipe" in data['intent']['parameters']:
        response = data['results'][1]['values']['news'][0]['detailurl']
    else:
        response = data['results'][0]['values']['text']

    return response